package com.hilda.twitter.timeline.ui.fragments;

import twitter4j.ResponseList;
import twitter4j.Status;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.hilda.twitter.R;
import com.hilda.twitter.parser.HildaParser;
import com.hilda.twitter.timeline.io.TaskListener;
import com.hilda.twitter.timeline.io.TimelineTask;
import com.hilda.twitter.timeline.ui.activities.DetailActivity;
import com.hilda.twitter.timeline.ui.adapters.TimelineAdapter;
import com.hilda.twitter.timeline.ui.widgets.PullToRefreshListView;
import com.hilda.twitter.timeline.ui.widgets.PullToRefreshListView.OnRefreshListener;
import com.hilda.twitter.timeline.utils.Log;

public class ListFragment extends android.support.v4.app.ListFragment implements
		TaskListener {

	// private TwitterApp mTwitterApp;
	private Context mContext;

	private TimelineTask mLoadTimelineTask;
	private PullToRefreshListView mListView;
	
	private HildaParser mHildaParser;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d("ListFragment: onCreate()");

		super.onCreate(savedInstanceState);

		mContext = this.getActivity().getApplicationContext();
		// mTwitterApp = new TwitterApp(mContext);
		setRetainInstance(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d("ListFragment: onActivityCreated()");

		super.onActivityCreated(savedInstanceState);

		if (mLoadTimelineTask == null) {
			mLoadTimelineTask = new TimelineTask(mContext, this);
			mLoadTimelineTask.execute();
		}

		mListView = (PullToRefreshListView) getListView();

		// Set a listener to be invoked when the list should be refreshed.

		if (!mListView.isThereAnOnRefreshListener()) {
			mListView.setOnRefreshListener(new OnRefreshListener() {
				@Override
				public void onRefresh() {

					if (mLoadTimelineTask == null
							|| mLoadTimelineTask.isFinished())
						mLoadTimelineTask = new TimelineTask(mContext,
								ListFragment.this);
					mLoadTimelineTask.execute();
				}
			});
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO use "position - 1" if using pulltorefreshlistview
		Status item = (Status) getListAdapter().getItem(position - 1);
		DetailFragment fragment = (DetailFragment) getFragmentManager()
				.findFragmentById(R.id.detailFragment);
		if (fragment != null && fragment.isInLayout()) {
			fragment.setText(item.getText());
		} else {
			Intent intent = new Intent(getActivity().getApplicationContext(),
					DetailActivity.class);
			intent.putExtra("value", item.getText());
			startActivity(intent);
		}
	}

	@Override
	public void onTaskFinished(ResponseList<Status> timeline) {
		if (timeline != null) {
			// TODO Download the UrlEntities[].ExpandedURL
			mHildaParser = new HildaParser(mContext, timeline);
			
			TimelineAdapter adapter = new TimelineAdapter(getActivity(),
					timeline);
			setListAdapter(adapter);
			mListView.onRefreshComplete();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return (ListView) inflater.inflate(R.layout.pulltorefresh_listview,
				null);
	}

}